<?php
session_start();
ob_start();
include_once 'include/common.php';
global $core, $category_id;
$core = new core();
if (!$core->CheckLogin()) {
    header('Location:login.php');
}
$_SESSION["photo_name_lists"] = array();
$_SESSION["photo_color_lists"]= array();
$html_category = '';
$category_id = 0;
$product_id = isset($_GET["id"]) ? intval($_GET["id"]) : 0;
if ($product_id > 0) {
    $product = $core->getProductById($product_id);
    if (!$product) {
        header("location:productList.php");
    } else {
        $category_id = $product->product_category;
        $extra_photo = $product->extra_photo;
        if ($extra_photo != "") {
            $extra_photo_list = explode(",", $extra_photo);
        }
    }
}

$html_category = show_category(0);

function show_category($parent_id = 0) {
    global $core, $category_id;
    $html = '';
    $category = $core->getCategoryByParentId($parent_id);

    if ($category) {
        foreach ($category as $cate) {
            $total_child_category = $core->getTotalChildCategoryById($cate->category_id);

            if ($total_child_category > 0) {
                $html.='<optgroup label="' . $cate->category_name . '">';
                $html.=show_category($cate->category_id);
                $html.='</optgroup>';
            } else {
                if ($category_id == $cate->category_id) {
                    $html.=' <option selected value="' . $cate->category_id . '">' . $cate->category_name . '</option>';
                } else {
                    $html.=' <option value="' . $cate->category_id . '">' . $cate->category_name . '</option>';
                }
            }
        }
    }
    return $html;
}
?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <!--
                Charisma v1.0.0

                Copyright 2012 Muhammad Usman
                Licensed under the Apache License v2.0
                http://www.apache.org/licenses/LICENSE-2.0

                http://usman.it
                http://twitter.com/halalit_usman
        -->
        <meta charset="utf-8">
        <title>Zen store</title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta name="description" content="Charisma, a fully featured, responsive, HTML5, Bootstrap admin template.">
        <meta name="author" content="Muhammad Usman">

        <!-- The styles -->
        <link id="bs-css" href="css/bootstrap-cerulean.css" rel="stylesheet">
        <style type="text/css">
            body {
                padding-bottom: 40px;
            }
            .sidebar-nav {
                padding: 9px 0;
            }
        </style>
        <link href="css/bootstrap-responsive.css" rel="stylesheet">
        <link href="css/charisma-app.css" rel="stylesheet">
        <link href="css/style.css" rel="stylesheet">
        <link href="css/jquery-ui-1.8.21.custom.css" rel="stylesheet">

        <link href='css/chosen.css' rel='stylesheet'>
        <link href='css/uniform.default.css' rel='stylesheet'>

        <link href='css/jquery.cleditor.css' rel='stylesheet'>


        <link href='css/jquery.iphone.toggle.css' rel='stylesheet'>

        <!-- CSS to style the file input field as button and adjust the Bootstrap progress bars -->
        <link rel="stylesheet" href="css/jquery.fileupload-ui.css">
        <!-- CSS adjustments for browsers with JavaScript disabled -->
        <noscript><link rel="stylesheet" href="css/jquery.fileupload-ui-noscript.css"></noscript>
        <link rel="shortcut icon" href="img/favicon.ico">
        <!-- blueimp Gallery styles -->
        <link rel="stylesheet" href='css/blueimp-gallery.min.css'>
        <script type="text/javascript">
            var array_extra_photo_name = new Array();

<?php
if ($product_id > 0 && $extra_photo != "") {
    for ($i = 0; $i < count($extra_photo_list); $i++) {
        ?>
                    array_extra_photo_name[<?php echo $i; ?>] = "<?php echo $extra_photo_list[$i]; ?>";

        <?php
    }
}
?>
        </script>
    </head>

    <body>
        <div id="page-wrap" class="page-wrap body-wrap ">
            <div id="page-tray" class="tray transition">
                <?php include_once ('new_menu.php'); ?>

                <div id="page-content" class="page-content transition profile-page">
                    <?php include_once ('navbar.php'); ?>
                    <div class="container-fluid">
                        <div class="row-fluid">
                            <div id="content" class="span12">       
<!--                                <div>
                                    <ul class="breadcrumb">
                                        <li>
                                            <a href="#">Home</a> <span class="divider">/</span>
                                        </li>
                                        <li>
                                            <a href="#">Forms</a>
                                        </li>
                                    </ul>
                                </div>-->

                                <div class="row-fluid sortable">
                                    <div class="box span12">
                                        <div class="box-header well" data-original-title>
                                            <h2><i class="icon-edit"></i> Thêm sản phẩm</h2>
                                            <div class="box-icon">

                                                <a href="#" class="btn btn-minimize btn-round"><i class="icon-chevron-up"></i></a>

                                            </div>
                                        </div>
                                        <div class="box-content">
                                            <div class="form-horizontal">
                                                <fieldset>
                                                    <div class="alert alert-error" id="alert-error-product_code" style="display:none"></div>
                                                    <div class="control-group">
                                                        <label class="control-label" for="product_code">Ref. * </label>
                                                        <div class="controls">

                                                            <div class="input-append">
                                                                <input type="text" size="16" name="product_code" id="product_code" value="<?php if ($product_id > 0) echo $product->product_code; ?>">
                                                                <button type="button" class="btn" id="get_product_info_by_code">Check Ref</button>
                                                            </div>
                                                        </div>
                                                    </div>
                                                    <div class="control-group">
                                                        <label class="control-label" for="product_name">Tên sản phẩm* </label>
                                                        <div class="controls">
                                                            <input type="text" class="span6 typeahead" id="product_name" value="<?php if ($product_id > 0) echo $product->product_name; ?>"  >

                                                        </div>
                                                    </div>

                                                    <div class="control-group">
                                                        <label class="control-label" for="link_web">Link web</label>
                                                        <div class="controls">
                                                            <input type="text" class="span6 typeahead" id="link_web" value="<?php if ($product_id > 0) echo $product->link_web; ?>"  >

                                                        </div>
                                                    </div>
                                                    <div class="control-group">
                                                        <label class="control-label" for="product_price">Giá bán</label>
                                                        <div class="controls">
                                                            <div class="input-prepend input-append">
                                                                <input type="text" class="span6 typeahead" id="product_price" value="<?php if ($product_id > 0) echo $product->product_price / 1000; ?>" >
                                                                <span class="add-on">000 VNĐ</span>
                                                            </div>

                                                        </div>
                                                    </div>

                                                    <div class="control-group">
                                                        <label class="control-label" for="store_price">Giá PT</label>
                                                        <div class="controls">
                                                            <input type="text" class="span6 typeahead" id="store_price" value="<?php if ($product_id > 0) echo $product->store_price; ?>" >

                                                        </div>
                                                    </div>
                                                    <div class="control-group">
                                                        <label class="control-label" for="stamp_price">Giá AT</label>
                                                        <div class="controls">
                                                            <input type="text" class="span6 typeahead" id="stamp_price" value="<?php if ($product_id > 0) echo $product->stamp_price; ?>"  >

                                                        </div>
                                                    </div>
                                                    <div class="control-group">
                                                        <label class="control-label" for="product_size">Size</label>

                                                        <div class="controls">
                                                            <input type="text" class="span6 typeahead" id="product_size"  value="<?php if ($product_id > 0) echo $product->product_size; ?>" >
                                                            <span class="help-inline">Cách nhau bởi dấu phẩy: M,L,XL</span>
                                                        </div>
                                                    </div>
                                                    <div class="control-group">
                                                        <label class="control-label" for="product_color">Màu*</label>
                                                        <div class="controls">
                                                            <input type="text" class="span6 typeahead" id="product_color" value="<?php if ($product_id > 0) echo $product->product_color; ?>" >
                                                            <span class="help-inline">Cách nhau bởi dấu phẩy: Red,Yellow</span>
                                                        </div>
                                                    </div>
                                                    <div class="control-group">
                                                        <label class="control-label" for="manufacture">Brand *</label>
                                                        <div class="controls">
                                                            
                                                            <select id="manufacture" >
                                                                <option value="Zara" <?php if ($product_id > 0) if($product->manufacture=='Zara') echo "selected=selected"; ?>>Zara</option>
                                                                <option value="Mango" <?php if ($product_id > 0) if($product->manufacture=='Mango') echo "selected=selected"; ?>>Mango</option>
                                                                <option value="Hm" <?php if ($product_id > 0) if($product->manufacture=='Hm') echo "selected=selected"; ?>>Hm</option>
                                                                <option value="Parfumes" <?php if ($product_id > 0) if($product->manufacture=='Parfumes') echo "selected=selected"; ?>>Parfumes</option>
                                                                <option value="Cosmetics" <?php if ($product_id > 0) if($product->manufacture=='Cosmetics') echo "selected=selected"; ?>>Cosmetics</option>


                                                            </select>
                                                        </div>
                                                    </div>

                                                    <div class="control-group">
                                                        <label class="control-label" for="product_category">Category*</label>
                                                        <div class="controls">
                                                            <select  id="product_category" >
                                                                <option value=""></option>
                                                                <?php echo $html_category; ?>

                                                            </select>
                                                        </div>
                                                    </div>
                                                    <div class="control-group">
                                                        <label class="control-label" for="fileInput">Chọn ảnh đại diện*</label>
                                                        <div class="controls">
                                                            <input type="hidden" name="file_name_temp" id="file_main_photo_temp" value="<?php if ($product_id > 0) echo $product->main_photo; ?>">
                                                            <input type="hidden" name="folder_temp" id="folder_temp" value="<?php if ($product_id > 0) echo "photo"; ?>">
                                                            <div class="input-append order_detail_new_product " style="margin-bottom: 10px">
                                                                <input type="text"  id="url_main_photo" value="Điền link tải ảnh">
                                                                <button type="button" class="btn" id="btn_upload_main_photo">upload photo</button>
                                                            </div>
                                                            Hoặc chọn ảnh từ máy:
                                                            <form class="form-horizontal form_upload_main_photo" name="form_upload_main_photo" id="form_upload_main_photo" method="post" enctype="multipart/form-data" action="ajax/uploadMainPhoto.php" target="upload_main_photo">
                                                                <input class="input-file uniform_on" id="main_photo" type="file" name="main_photo">
                                                                <a href="javascript:void(0)" class="btn btn-danger" id="btn_delete_main_photo">
                                                                    <i class="icon-trash icon-white"></i> 
                                                                    Delete
                                                                </a>
                                                            </form>
                                                            <iframe id="upload_main_photo" name="upload_main_photo" src=""></iframe>
                                                            <?php if ($product_id > 0) {
                                                                ?>
                                                                <div style="width:200px; height:200px; overflow: hidden;display: block;" id="show_main_photo"><img src="<?php echo "photo/thumb_photo/200_" . $product->main_photo; ?>" width=200px /></div>
                                                                <?php
                                                            } else {
                                                                ?>
                                                                <div style="width:200px; height:200px; overflow: hidden;display: none;" id="show_main_photo"></div>
                                                                <?php
                                                            }
                                                            ?>
                                                        </div>
                                                    </div>    
                                                    <div class="control-group">
                                                        <label class="control-label" for="fileInput">Chọn thêm ảnh phụ</label>
                                                        <div class="controls">
                                <!--                           <input data-no-uniform="true" type="file" name="file_upload" id="file_upload" />-->
                                                            <form id="fileupload" action="" method="POST" enctype="multipart/form-data" >
                                                                <!-- Redirect browsers with JavaScript disabled to the origin page -->
                                                                <noscript><input type="hidden" name="redirect" value=""></noscript>
                                                                <!-- The fileupload-buttonbar contains buttons to add/delete files and start/cancel the upload -->
                                                                <div class=" fileupload-buttonbar">
                                                                    <div class="col-lg-7">
                                                                        <!-- The fileinput-button span is used to style the file input field as button -->
                                                                        <input type="file" id="extra_photo" class="input-file uniform_on" name="files[]" multiple>

                                                                        <button type="submit" class="btn btn-primary start">
                                                                            <i class="glyphicon glyphicon-upload"></i>
                                                                            <span>Start upload</span>
                                                                        </button>
                                                                        <button type="reset" class="btn btn-warning cancel">
                                                                            <i class="glyphicon glyphicon-ban-circle"></i>
                                                                            <span>Cancel upload</span>
                                                                        </button>
                                                                        <button type="button" class="btn btn-danger delete">
                                                                            <i class="glyphicon glyphicon-trash"></i>
                                                                            <span>Delete</span>
                                                                        </button>
                                                                        <input type="checkbox" class="toggle">
                                                                        <!-- The loading indicator is shown during file processing -->
                                                                        <span class="fileupload-loading"></span>
                                                                    </div>
                                                                    <!-- The global progress information -->
                                                                    <div class="col-lg-5 fileupload-progress fade">
                                                                        <!-- The global progress bar -->
                                                                        <div class="progress progress-striped active" role="progressbar" aria-valuemin="0" aria-valuemax="100">
                                                                            <div class="progress-bar progress-bar-success" style="width:0%;"></div>
                                                                        </div>
                                                                        <!-- The extended global progress information -->
                                                                        <div class="progress-extended">&nbsp;</div>
                                                                    </div>
                                                                </div>

                                                                <!-- The table listing the files available for upload/download -->
                                                                <table role="presentation" class="table table-striped"><tbody class="files"></tbody></table>
                                                            </form>
                                                            <?php
                                                            if ($product_id > 0 && $extra_photo != "") {
                                                                echo '<table class="table table-striped"><tbody class="files">';
                                                                foreach ($extra_photo_list as $extra_photo_name) {
                                                                    echo '<tr class="template-download fade in">
                                                        <td>
                                                            <span class="preview">
                                                                <a data-gallery="" download="' . $extra_photo_name . '" title="' . $extra_photo_name . '" href="photo/extra_photo/' . $extra_photo_name . '"><img src="photo/extra_photo/thumb_extra_photo/' . $extra_photo_name . '"></a>
                                                            </span>
                                                        </td>
                                                        <td>
                                                            <p class="name">
                                                                 <a data-gallery="" download="' . $extra_photo_name . '" title="' . $extra_photo_name . '" href="photo/extra_photo/' . $extra_photo_name . '">' . $extra_photo_name . '</a>
                                                            </p>
                                                        </td>                                                        
                                                        <td>
                                                            <button type="button" class="btn btn-danger btn_delete_extra_photo" style="opacity: 1;" rel="' . $extra_photo_name . '">
                                                                <i class="glyphicon glyphicon-trash"></i>
                                                                <span>Delete</span>
                                                            </button>                                                       
                                                        </td>
                                                      </tr>';
                                                                }
                                                                echo '</tbody></table>';
                                                            }
                                                            ?>
                                                            <!-- The blueimp Gallery widget -->
                                                            <div id="blueimp-gallery" class="blueimp-gallery blueimp-gallery-controls" data-filter=":even">
                                                                <div class="slides"></div>
                                                                <h3 class="title"></h3>
                                                                <a class="prev">‹</a>
                                                                <a class="next">›</a>
                                                                <a class="close">×</a>
                                                                <a class="play-pause"></a>
                                                                <ol class="indicator"></ol>
                                                            </div>
                                                        </div>
                                                    </div>    
                                                    <div class="control-group">
                                                        <label class="control-label" for="description">Miêu tả sản phẩm</label>
                                                        <div class="controls">
                                                            <textarea class="cleditor" id="description" rows="3"><?php if ($product_id > 0) echo $product->description; ?></textarea>
                                                        </div>
                                                    </div>

                                                    <div class="alert alert-error" id="alert-error-product" style="display:none">
                                                    </div>
                                                    <div class="form-actions">
                                                        <input type="hidden" name="product_id" id="product_id" value="<?php if ($product_id > 0) echo $product_id; ?>">
                                                        <button type="button" class="btn btn-primary" id="save_product">Save changes</button>
                                                        <button type="button" class="btn" id="reset_form">Cancel</button>
                                                    </div>
                                                </fieldset>
                                            </div>   

                                        </div>
                                    </div><!--/span-->

                                </div><!--/row-->


                                <div class="row-fluid sortable">
                                    <div class="box span12">
                                        <div class="box-header well" data-original-title>
                                            <h2><i class="icon-edit"></i> Import sản phẩm</h2>
                                            <div class="box-icon">
                                                <a href="#" class="btn btn-setting btn-round"><i class="icon-cog"></i></a>
                                                <a href="#" class="btn btn-minimize btn-round"><i class="icon-chevron-up"></i></a>
                                                <a href="#" class="btn btn-close btn-round"><i class="icon-remove"></i></a>
                                            </div>
                                        </div>
                                        <div class="box-content">
                                            <form class="form-horizontal form_upload_file" name="form_upload_file" id="form_upload_file" method="post" enctype="multipart/form-data" action="ajax/uploadFileProduct.php" target="upload_target">
                                                <fieldset>
                                                    <div class="control-group">
                                                        <label class="control-label" for="fileInput">File input</label>
                                                        <div class="controls upload-btn">
                                                            <input class="input-file uniform_on" id="input_upload_file" type="file" name="file">
                                                        </div>
                                                    </div> 
                                                    <div class="form-actions">
                                                        <button type="submit" class="btn btn-primary">Save changes</button>
                                                        <button class="btn">Cancel</button>
                                                    </div>
                                                </fieldset>
                                            </form>
                                            <iframe id="upload_target" name="upload_target" src=""></iframe>

                                        </div>
                                    </div><!--/span-->

                                </div><!--/row-->


                            </div><!--/fluid-row-->
                            <?php include_once ('footer.php'); ?>

                        </div><!--/.fluid-container-->
                    </div>
                </div><!--/#page-content-->
            </div>
        </div>
        <!-- external javascript
        ================================================== -->
        <!-- Placed at the end of the document so the pages load faster -->

        <!-- jQuery -->
        <script src="js/jquery-1.7.2.min.js"></script>
        <!-- jQuery UI -->
        <script src="js/jquery-ui-1.8.21.custom.min.js"></script>
        <!-- transition / effect library -->
        <script src="js/bootstrap-transition.js"></script>
        <!-- alert enhancer library -->
        <script src="js/bootstrap-alert.js"></script>
        <!-- accordion library (optional, not used in demo) -->
        <script src="js/bootstrap-collapse.js"></script>
        <!-- library for creating tabs -->
        <script src="js/bootstrap-tab.js"></script>
        <!-- library for advanced tooltip -->
        <script src="js/bootstrap-tooltip.js"></script>
        <!-- popover effect library -->
        <script src="js/bootstrap-popover.js"></script>

        <!-- library for cookie management -->
        <script src="js/jquery.cookie.js"></script>


        <!-- select or dropdown enhancer -->
        <script src="js/jquery.chosen.min.js"></script>
        <!-- checkbox, radio, and file input styler -->
        <script src="js/jquery.uniform.min.js"></script>

        <!-- rich text editor library -->
        <script src="js/jquery.cleditor.min.js"></script>

        <!-- for iOS style toggle switch -->
        <script src="js/jquery.iphone.toggle.js"></script>
        <!-- autogrowing textarea plugin -->
        <script src="js/jquery.autogrow-textarea.js"></script>
        <!-- multiple file upload plugin -->

        <!-- history.js for cross-browser state change on ajax -->
        <script src="js/jquery.history.js"></script>
        <!-- application script for Charisma demo -->
        <script src="js/common.js"></script>

        <script id="template-upload" type="text/x-tmpl">
            {% for (var i=0, file; file=o.files[i]; i++) { %}
            <tr class="template-upload fade">
            <td>
            <span class="preview"></span>
            </td>
            <td>
            <p class="name">{%=file.name%}</p>
            {% if (file.error) { %}
            <div><span class="label label-danger">Error</span> {%=file.error%}</div>
            {% } %}
            </td>
            <td>
            <p class="size">{%=o.formatFileSize(file.size)%}</p>
            {% if (!o.files.error) { %}
            <div class="progress progress-striped active" role="progressbar" aria-valuemin="0" aria-valuemax="100" aria-valuenow="0"><div class="progress-bar progress-bar-success" style="width:0%;"></div></div>
            {% } %}
            </td>
            <td>
            {% if (!o.files.error && !i && !o.options.autoUpload) { %}
            <button class="btn btn-primary start">
            <i class="glyphicon glyphicon-upload"></i>
            <span>Start</span>
            </button>
            {% } %}
            {% if (!i) { %}
            <button class="btn btn-warning cancel">
            <i class="glyphicon glyphicon-ban-circle"></i>
            <span>Cancel</span>
            </button>
            {% } %}
            </td>
            <td>
                    {%var filename = file.name.replace(".","");%}
                    <input class="color_name" type="text" name="color_name_{%=filename%}" value=""> Điền tên màu tương ứng của sản phẩm 
            </td>
            </tr>
            {% } %}
        </script>
        <!-- The template to display files available for download -->
        <script id="template-download" type="text/x-tmpl">
            {% for (var i=0, file; file=o.files[i]; i++) { %}
            <tr class="template-download fade">
            <td>
            <span class="preview">
            {% if (file.thumbnailUrl) { %}
            <a href="{%=file.url%}" title="{%=file.name%}" download="{%=file.name%}" data-gallery><img src="{%=file.thumbnailUrl%}"></a>
            {% } %}
            </span>
            </td>
            <td>
            <p class="name">
            {% if (file.url) { %}
            <a href="{%=file.url%}" title="{%=file.name%}" download="{%=file.name%}" {%=file.thumbnailUrl?'data-gallery':''%}>{%=file.name%}</a>
            {% } else { %}
            <span>{%=file.name%}</span>
            {% } %}
            </p>
            {% if (file.error) { %}
            <div><span class="label label-danger">Error</span> {%=file.error%}</div>
            {% } %}
            </td>
            <td>
            <span class="size">{%=o.formatFileSize(file.size)%}</span>
            </td>
            <td>
            {% if (file.deleteUrl) { %}
            <button class="btn btn-danger delete" data-type="{%=file.deleteType%}" data-url="{%=file.deleteUrl%}"{% if (file.deleteWithCredentials) { %} data-xhr-fields='{"withCredentials":true}'{% } %}>
            <i class="glyphicon glyphicon-trash"></i>
            <span>Delete</span>
            </button>
            <input type="checkbox" name="delete" value="1" class="toggle">
            {% } else { %}
            <button class="btn btn-warning cancel">
            <i class="glyphicon glyphicon-ban-circle"></i>
            <span>Cancel</span>
            </button>
            {% } %}
            </td>
            </tr>
            {% } %}
        </script>
        <!-- The jQuery UI widget factory, can be omitted if jQuery UI is already included -->
        <script src="js/vendor/jquery.ui.widget.js"></script>
        <!-- The Templates plugin is included to render the upload/download listings -->
        <script src="js/tmpl.min.js"></script>
        <!-- The Load Image plugin is included for the preview images and image resizing functionality -->
        <script src="js/load-image.min.js"></script>
        <!-- The Canvas to Blob plugin is included for image resizing functionality -->
        <script src="js/canvas-to-blob.min.js"></script>
        <!-- Bootstrap JS is not required, but included for the responsive demo navigation -->
        <script src="js/bootstrap.min.js"></script>
        <!-- blueimp Gallery script -->
        <script src="js/jquery.blueimp-gallery.min.js"></script>
        <!-- The Iframe Transport is required for browsers without support for XHR file uploads -->
        <script src="js/jquery.iframe-transport.js"></script>
        <!-- The basic File Upload plugin -->
        <script src="js/jquery.fileupload.js"></script>
        <!-- The File Upload processing plugin -->
        <script src="js/jquery.fileupload-process.js"></script>
        <!-- The File Upload image preview & resize plugin -->
        <script src="js/jquery.fileupload-image.js"></script>
        <!-- The File Upload audio preview plugin -->
        <script src="js/jquery.fileupload-audio.js"></script>
        <!-- The File Upload video preview plugin -->
        <script src="js/jquery.fileupload-video.js"></script>
        <!-- The File Upload validation plugin -->
        <script src="js/jquery.fileupload-validate.js"></script>
        <!-- The File Upload user interface plugin -->
        <script src="js/jquery.fileupload-ui.js"></script>
        <!-- The main application script -->
        <script src="js/main.js?t=1"></script>
        <script src="js/addProduct.js?t=7"></script>
    </body>
</html>

